package cn.dm.client;

import cn.dm.MessageProducer;
import cn.dm.RingBufferWorkerPoolFactory;
import cn.dm.entity.TranslatorData;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class ClientHandler extends ChannelInboundHandlerAdapter {

    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

        /**
         * 写法一
         * (与ServerHandler要对应)
         */
//        try {
//            TranslatorData response = (TranslatorData) msg;
//            System.err.println("Client端: id= " + response.getId()
//                    + ", name= " + response.getName()
//                    + ", message= " + response.getMessage());
//        } finally {
//            //一定要注意 用完了缓存 要进行释放
//            ReferenceCountUtil.release(msg);
//        }

        /**
         * 写法二(优化)
         * (与ServerHandler要对应)
         */
        TranslatorData response = (TranslatorData) msg;
        String producerId = "code:seesionId:002";
        MessageProducer messageProducer = RingBufferWorkerPoolFactory.getInstance().getMessageProducer(producerId);
        messageProducer.onData(response, ctx);

    }
}
